package com.neusoft.hifly.information.article.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.neusoft.hifly.commons.exception.SystemErrorException;
import com.neusoft.hifly.core.basic.service.impl.CTSaaSBaseServiceImpl;
import com.neusoft.hifly.core.log.annotation.LogType;
import com.neusoft.hifly.core.log.annotation.OperationLog;
import com.neusoft.hifly.dynamicds.multi.annotation.MultiDynamicRoutingDataSource;
import com.neusoft.hifly.information.article.dao.IArtToColDao;
import com.neusoft.hifly.information.article.entity.ArtToColObj;
import com.neusoft.hifly.information.article.service.IArtToColService;

/**
 * Title: 文章资讯服务
 * <p>
 * Description: 文章与栏目关联 Service
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
@Service
@MultiDynamicRoutingDataSource
public class ArtToColServiceImpl extends CTSaaSBaseServiceImpl implements IArtToColService {
	@Autowired(required = false)
	private IArtToColDao artToColDao;

	/**
	 * 保存对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param obj
	 *            对象
	 * @throws SystemErrorException
	 *             自定义异常
	 */
	@Override
	@OperationLog(method = LogType.UPDATE, msg = "文章与栏目关联")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
	public void saveOrUpdate(final String organCode, final long organLogicId, final ArtToColObj obj)
			throws SystemErrorException {
		if (StringUtils.isEmpty(organCode) || obj == null) {
			return;
		}

		final ArtToColObj oldObj = artToColDao.getByLogicId(organCode, organLogicId, obj.getColumnLogicId(),
				obj.getArticleLogicId());

		obj.setOrganLogicId(organLogicId);

		// 有ID的话要判断是否保存还是更新
		if (oldObj == null) {
			// 保存

			this.setForm(obj, obj);

			artToColDao.insert(obj);

		} else {
			// 更新

			this.setForm(obj, oldObj);

			artToColDao.updateById(oldObj);

		}
	}

	/**
	 * 根据ID删除对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param columnLogicId
	 *            栏目逻辑ID
	 * @param articleLogicId
	 *            文章逻辑ID
	 */
	@Override
	@OperationLog(method = LogType.DELETE, msg = "文章与栏目关联")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class, RuntimeException.class })
	public void deleteByLogicId(final String organCode, final long organLogicId, final long columnLogicId,
			final long articleLogicId) {
		if (StringUtils.isEmpty(organCode)) {
			return;
		}

		artToColDao.deleteByLogicId(organCode, organLogicId, columnLogicId, articleLogicId);
	}

	/**
	 * 根据id查询对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param columnLogicId
	 *            栏目逻辑ID
	 * @param articleLogicId
	 *            文章逻辑ID
	 * @return 对象
	 */
	@Override
	public ArtToColObj getByLogicId(final String organCode, final long organLogicId, final long columnLogicId,
			final long articleLogicId) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		return artToColDao.getByLogicId(organCode, organLogicId, columnLogicId, articleLogicId);
	}

	/**
	 * 查询所有对象
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @return 对象集合
	 */
	@Override
	public List<ArtToColObj> findAll(final String organCode, final long organLogicId) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		return artToColDao.findAll(organCode, organLogicId);
	}

	/**
	 * 根据条件查询
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param property
	 *            参数。其中isLike:Boolean 是否模糊查询
	 * @return 集合
	 */
	@Override
	public List<ArtToColObj> search(final String organCode, final long organLogicId, Map<String, Object> property) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		if (property == null) {
			property = new HashMap<String, Object>(1);
			property.put("organLogicId", organLogicId);
		}
		final QueryWrapper<ArtToColObj> queryWrapper = new QueryWrapper<ArtToColObj>();
		replaceProperty(property);
		createSql(property, queryWrapper, new String[] { "ARTICLE_ORDER" });
		return artToColDao.selectList(queryWrapper);
	}

	/**
	 * 根据条件查询
	 *
	 * @param organCode
	 *            租户编码
	 * @param organLogicId
	 *            租户逻辑ID
	 * @param offset
	 *            当前页数
	 * @param limit
	 *            分页长度
	 * @param property
	 *            参数。其中isLike:Boolean 是否模糊查询
	 * @return 返回Map
	 */
	@Override
	public IPage<ArtToColObj> search(final String organCode, final long organLogicId, final int offset, final int limit,
			Map<String, Object> property) {
		if (StringUtils.isEmpty(organCode)) {
			return null;
		}
		if (property == null) {
			property = new HashMap<String, Object>(1);
			property.put("organLogicId", organLogicId);
		}
		final IPage<ArtToColObj> page = new Page<ArtToColObj>(offset, limit);
		final QueryWrapper<ArtToColObj> queryWrapper = new QueryWrapper<ArtToColObj>();
		replaceProperty(property);
		createSql(property, queryWrapper, new String[] { "ARTICLE_ORDER" });
		return artToColDao.selectPage(page, queryWrapper);
	}

	/**
	 * 复制信息
	 *
	 * @param fromObj
	 *            原信息
	 * @param toObj
	 *            新信息
	 */
	private void setForm(final ArtToColObj fromObj, final ArtToColObj toObj) {
		toObj.setOrganLogicId(fromObj.getOrganLogicId());
		toObj.setArticleLogicId(fromObj.getArticleLogicId());
		toObj.setColumnLogicId(fromObj.getColumnLogicId());
		toObj.setArticleOrder(fromObj.getArticleOrder());

	}

	/**
	 * 将对象属性替换成数据库列
	 *
	 * @param property
	 *            参数
	 */
	private void replaceProperty(final Map<String, Object> property) {
		if (property != null) {
			if (property.containsKey("organLogicId")) {
				property.put("ORGAN_LOGIC_ID", property.get("organLogicId"));
				property.remove("organLogicId");
			}
			if (property.containsKey("articleLogicId")) {
				property.put("ARTICLE_LOGIC_ID", property.get("articleLogicId"));
				property.remove("articleLogicId");
			}
			if (property.containsKey("columnLogicId")) {
				property.put("COLUMN_LOGIC_ID", property.get("columnLogicId"));
				property.remove("columnLogicId");
			}
			if (property.containsKey("articleOrder")) {
				property.put("ARTICLE_ORDER", property.get("articleOrder"));
				property.remove("articleOrder");
			}

		}
	}
}